<template>
  <div class="fillcontain">
    <div class="psd-container">
      <el-form
        ref="ruleForm"
        :rules="rules"
        class="small-space"
        :model="ruleForm"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="原密码" prop="oldPassword">
          <el-input v-model="ruleForm.oldPassword" type="password" auto-complete="off" />
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="ruleForm.newPassword" type="password" />
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="ruleForm.confirmPassword" type="password" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button type="primary" @click="confirm">确 定</el-button>
        <el-button>取 消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { xhrResetPassword } from '@api/user'

export default {
  name: 'ResetPassword',
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      ruleForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      rules: {
        newPassword: [{ validator: validatePass, trigger: 'blur' }],
        confirmPassword: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  created() {},
  methods: {
    confirm() {
      this.$refs['ruleForm'].validate(valid => {
        if (valid) {
          const params = {
            oldPassword: this.ruleForm.oldPassword,
            newPassword: this.ruleForm.newPassword
          }
          xhrResetPassword(params).then(resp => {
            if (resp.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success'
              })
              this.$store.dispatch('user/logout')
            } else {
              this.$message({
                message: resp.msg,
                type: 'error'
              })
            }
          })
        } else {
          console.log('error submit!!')
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.psd-container {
  width: 400px;
  margin: 40px auto;
}
</style>
